package com.snuproject.lgt.imgtrans;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class DrawPaper extends View {
	
	Paint paint = null;
	Path path = null;
	Path newPath = null;
	BitmapDrawable bg_drawable = null;
	Bitmap src = null;
	
	Bitmap result = null;
	Canvas result_cv = null;

	public DrawPaper(Context context, Bitmap bg) {
		super(context);
		
		paint = new Paint();
        paint.setStrokeWidth(5);
        paint.setColor(Color.BLACK);
        paint.setStyle(Paint.Style.STROKE);
		
		path = new Path();
		newPath = new Path();
		src = bg;
		bg_drawable = new BitmapDrawable(bg);
		
		result = Bitmap.createBitmap(bg_drawable.getIntrinsicWidth(), bg_drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
		result.eraseColor(Color.WHITE);
		
		result_cv = new Canvas(result);
	}

    @Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		
		// draw bg
		int width = bg_drawable.getIntrinsicWidth();
		int height = bg_drawable.getIntrinsicHeight();
		int left = (canvas.getWidth()-width)/2;
		int top = (canvas.getHeight()-height)/2;
		bg_drawable.setBounds(left, top, left + width - 1, top + height - 1);
		bg_drawable.draw(canvas);
		
		// draw path
        canvas.drawPath(path, paint);
        
        newPath.set(path);
        newPath.offset(-left, -top);
        result_cv.drawPath(newPath, paint);
	}
    
    public Bitmap getTransparent() {
    	
    	Bitmap result_cp = Bitmap.createScaledBitmap(result, src.getWidth(), src.getHeight(), true);
    	Bitmap src_cp = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888); 

        for(int j=0; j<result_cp.getHeight(); j++){
                for(int i=0; i<result_cp.getWidth(); i++){
                        if(result_cp.getPixel(i, j)==Color.BLACK)
                                break;
                        result_cp.setPixel(i, j, Color.BLACK);    
                }
        }
        
        for(int j=0; j<result_cp.getHeight(); j++){
                for(int i=result_cp.getWidth()-1; i>0; i--){
                        if(result_cp.getPixel(i, j)==Color.BLACK)
                                break;
                        result_cp.setPixel(i, j, Color.BLACK);    
                }
        }
        
        for( int j=0; j<src_cp.getHeight(); j++) {
        	for(int i=0; i<src_cp.getWidth(); i++) {
        		if(result_cp.getPixel(i, j) == Color.BLACK) {
        			src_cp.setPixel(i, j, Color.TRANSPARENT);
        		} else {
        			src_cp.setPixel(i, j, src.getPixel(i, j));
        		}
        	}
        }
    	
    	return src_cp;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub

            float x = event.getX();
            float y = event.getY();
            
            
            switch(event.getAction()){

            case MotionEvent.ACTION_DOWN:
                    path.moveTo(x, y);
                    break;
            case MotionEvent.ACTION_MOVE:
                    x =event.getX();
                    y =event.getY();
                    path.lineTo(x, y);
                    break;
            case MotionEvent.ACTION_UP:
                    break;
            }
            invalidate();
            return true;
    }    
}
